using UnityEngine;
using System.Collections;

enum buttonState
{
	idle,
	hover,
	clicked
}

public class buttons : MonoBehaviour
{
	
	public Texture2D[] stateTextures;
	buttonState currentButtonState;
	
	public AudioClip clickSound;
	public AudioClip hoverSound;
	
	bool mouseIN;
	bool mouseDown;
	
	AudioSource sPlayer;
	
	// Use this for initialization
	void Start () 
	{
		sPlayer = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AudioSource>();
	}
	
	// Update is called once pewr framew
	void Update () 
	{
		if(mouseIN)
		{
			if(mouseDown)
			{
				try
				{
					if(currentButtonState != buttonState.clicked)
					{
						renderer.material.mainTexture = stateTextures[2];
						currentButtonState = buttonState.clicked;
					}
				}
				catch
				{
					if(currentButtonState != buttonState.hover)
					{
						renderer.material.mainTexture = stateTextures[1];
						currentButtonState = buttonState.hover;
					}
				}
			}
			else
			{
				if(currentButtonState != buttonState.hover)
				{
					renderer.material.mainTexture = stateTextures[1];
					currentButtonState = buttonState.hover;
				}
			}
		}
		else
		{
			if(currentButtonState != buttonState.idle)
			{
				renderer.material.mainTexture = stateTextures[0];
				currentButtonState = buttonState.idle;
			}
		}
	}
	
	public virtual void doFunction()
	{
	}
	
	void OnMouseEnter()
	{
		if(hoverSound != null)
		{
			sPlayer.GetComponent<AudioSource>().PlayOneShot(hoverSound);
		}
		mouseIN = true;
	}
	void OnMouseExit()
	{
		mouseIN = false;
	}
	void OnMouseDown()
	{
		if(clickSound != null)
		{
			sPlayer.GetComponent<AudioSource>().PlayOneShot(clickSound);
		}
		mouseDown = true;
	}
	void OnMouseUp()
	{
		mouseDown = false;
		doFunction();
	}
	

}
